package es.udc.emarketapp.http.controller.actions.general;


import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import es.udc.emarketapp.emarket.model.usersFacade.exceptions.IncorrectPasswordException;
import es.udc.emarketapp.http.controller.session.SessionManager;
import es.udc.emarketapp.http.view.actionForms.ChangePasswordForm;
import es.udc.fbellas.j2ee.util.exceptions.InternalErrorException;
import es.udc.fbellas.j2ee.util.struts.action.DefaultAction;


public class ChangePasswordAction extends DefaultAction {


    public ActionForward doExecute(ActionMapping mapping,
            ActionForm form, HttpServletRequest request,
            HttpServletResponse response)
            throws IOException, ServletException, InternalErrorException {

        /* Get data. */

        System.out.println("ChangePassword ........");

        ChangePasswordForm changePassForm = (ChangePasswordForm) form;
        String oldPass = changePassForm.getOldPassword();
        String newPass = changePassForm.getNewPassword();

        System.out.println("oldPassword" + oldPass);
        System.out.println("newPassword" + newPass);

        ActionMessages errors = new ActionMessages();

        try {
            SessionManager.changePassword(request, response, oldPass, newPass);
        } catch (IncorrectPasswordException e) {
            errors.add("oldPassword", new ActionMessage(
                "ErrorMessages.password.incorrect"));
        }

          /* Return ActionForward. */
        if (errors.isEmpty()) {
            return mapping.findForward("Success");
        } else {
            saveErrors(request, errors);
            return new ActionForward(mapping.getInput());
        }

       
    }
}
